package org.zhengquan.filter;

import org.zhengquan.common.Const;
import org.zhengquan.common.RequestHolder;
import org.zhengquan.pojo.SysUser;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhengquan
 * @date 2017/11/1
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        SysUser sysUser = (SysUser) req.getSession().getAttribute(Const.CURRENT_USER);
        if (sysUser == null) {
            resp.sendRedirect(Const.SIGN_IN_PAGE);
            return;
        }
        RequestHolder.add(sysUser);
        RequestHolder.add(req);
        filterChain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
